package zmee.pur.opplugin;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.excel.util.ObjectUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.basedata.BaseDataServiceHelper;
import kd.bos.servicehelper.permission.PermissionServiceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: SupplierregOp
 * @description: TODO 类描述
 * @author: zilongluo
 * @date: 2022/12/5
 * @Version: 1.0
 **/
public class SupplierregOp extends AbstractOperationServicePlugIn {
    private final static Log logger = LogFactory.getLog(SupplierregOp.class);
    private final String FORMBILL = "bd_supplier";


    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        //e.getFieldKeys().add("zmee_applyorg");
        //e.getFieldKeys().add("org");
        //e.getFieldKeys().add("number");
        e.getFieldKeys().add("supplier");
    }

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {

        //审核后根据申请组织自动分配供应商
        if(StringUtils.equals(e.getOperationKey(),"common")){
            DynamicObject[] dataEntities = e.getDataEntities();
            if(!ObjectUtils.isEmpty(dataEntities)){
                for (DynamicObject dataEntity : dataEntities) {
                    DynamicObject supplier = dataEntity.getDynamicObject("supplier");
                    String supplierNumber = supplier.getString("number");
                    DynamicObject supplierReg = BusinessDataServiceHelper.loadSingle("srm_supplierreg", "id,zmee_applyorg,phone",
                            new QFilter[]{new QFilter("number", "=", supplierNumber)});
                    DynamicObject zmee_applyorg = supplierReg.getDynamicObject("zmee_applyorg");
                    DynamicObject org = BusinessDataServiceHelper.loadSingle("bos_org", "id", new QFilter[]{new QFilter("number", QCP.equals, "26501")});
                    if(!ObjectUtils.isEmpty(zmee_applyorg)){
                        List<Long> orgIds = new ArrayList<Long>(16);

                        orgIds.add(zmee_applyorg.getLong("id"));

                        DynamicObject bdSupplier = supplier.getDynamicObject("supplier");
                        DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(FORMBILL, "id", new QFilter[]{new QFilter(
                                "number", QCP.equals, bdSupplier.getString("number"))});
                        DynamicObject srmSupplier = BusinessDataServiceHelper.loadSingle("srm_supplier", "id", new QFilter[]{new QFilter(
                                "supplier.number", QCP.equals, bdSupplier.getString("number"))});

                        if(!ObjectUtils.isEmpty(dynamicObject) && !ObjectUtils.isEmpty(srmSupplier)){
                            List<Long> detailIds = new ArrayList<Long>(16);
                            detailIds.add(dynamicObject.getLong("id"));

                            List<Long> srmDetailIds = new ArrayList<Long>(16);
                            srmDetailIds.add(srmSupplier.getLong("id"));


                            Map<Long, Map<Long, String>> resultMap  = BaseDataServiceHelper.batchAssignWithDetail(FORMBILL, org.getLong("id"), detailIds, orgIds);
                            Map<Long, Map<Long, String>> srmResultMap = BaseDataServiceHelper.batchAssignWithDetail("srm_supplier",
                                    org.getLong("id"), srmDetailIds, orgIds);

                            if (resultMap.isEmpty() && srmResultMap.isEmpty()) {
                                String phone = supplierReg.getString("phone");
                                QFilter filter = new QFilter("phone", "=", phone).or("email", "=", phone);
                                DynamicObject user = BusinessDataServiceHelper.loadSingle("bos_user", "id", filter.toArray());
                                if (!ObjectUtils.isEmpty(user)) {
                                    Map<Long, List<String>> assignInfo = new HashMap<>(8);
                                    List<String> roleId = new ArrayList<>();
                                    roleId.add("1PI8PIG1LC=J");
                                    assignInfo.put(zmee_applyorg.getLong("id"), roleId);
                                    Map<Long, Boolean> orgInfo = new HashMap<>(8);
                                    orgInfo.put(zmee_applyorg.getLong("id"), false);
                                    boolean flag = PermissionServiceHelper.userAssignRole(user.getLong("id"), "bos_org", assignInfo,
                                            orgInfo, true);
                                }
                                logger.info("SupplierregOp供应商" + dynamicObject.getString("number") + "分配组织" + org.getString("name") + "成功");

                            }else{
                                logger.info("SupplierregOp记录供应商"+dynamicObject.getString("number")+"分配失败原因"+resultMap);
                            }


                        }else{
                            logger.info("SupplierregOp供应商数据未生成");
                        }



                    }

                }
            }

        }

    }
}
